package com.dialogutils;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.tools.R;

import java.lang.reflect.Method;

public class BaseDialog extends AlertDialog {
    private static final String TAG = "BaseDialog";
    protected Context context;
    private View view;
    private int width;

    public BaseDialog(Context context) {
        this(context, R.style.Dialog_Common);
    }

    public BaseDialog(Context context, int res) {
        super(context, res);
        this.context = context;
        setCancelable(isCancelable());
        setCanceledOnTouchOutside(isCanceledOnTouchOutside());

        width = (int) (getScreenWidth(this.context) * setWidthWidget());
        view = loadView();
        initView(view);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout.LayoutParams params;
        if (!isSquare()) {
            params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams
                    .WRAP_CONTENT, 0);
        } else {
            params = new LinearLayout.LayoutParams(width, width, 0);
        }
        setContentView(view, params);
    }

    /**
     * 加载弹窗布局文件
     *
     * @return
     */
    protected View loadView() {
        return new TextView(context);
    }

    /**
     * 加载弹窗布局
     *
     * @param view
     */
    protected void initView(View view) {
    }

    /**
     * 设置弹窗宽
     *
     * @return
     */
    protected float setWidthWidget() {
        return 0.7f;
    }

    /**
     * 正方形弹窗
     */
    protected boolean isSquare() {
        return false;
    }

    /**
     * 是否可以关闭
     *
     * @return
     */
    protected boolean isCancelable() {
        return true;
    }

    /**
     * 是否可以点击外部进行关闭
     *
     * @return
     */
    protected boolean isCanceledOnTouchOutside() {
        return true;
    }

    @Override
    public void show() {
        super.show();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

    protected int getScreenWidth(Context context) {
        return getScreenSize(context)[0];
    }

    protected int getScreenHeight(Context context) {
        return getScreenSize(context)[1];
    }

    private int[] getScreenSize(Context context) {
        WindowManager windowManager;
        try {
            windowManager = (WindowManager) context.getSystemService("window");
        } catch (Throwable var6) {
            Log.w(TAG, var6);
            windowManager = null;
        }

        if (windowManager == null) {
            return new int[]{0, 0};
        } else {
            Display display = windowManager.getDefaultDisplay();
            if (Build.VERSION.SDK_INT < 13) {
                DisplayMetrics t1 = new DisplayMetrics();
                display.getMetrics(t1);
                return new int[]{t1.widthPixels, t1.heightPixels};
            } else {
                try {
                    Point t = new Point();
                    Method method = display.getClass().getMethod("getRealSize", Point.class);
                    method.setAccessible(true);
                    method.invoke(display, t);
                    return new int[]{t.x, t.y};
                } catch (Throwable var5) {
                    Log.w(TAG, var5);
                    return new int[]{0, 0};
                }
            }
        }
    }
}